package cn.caskm.souvenirshopsystem.controller.api;

import cn.caskm.souvenirshopsystem.domain.Result;
import cn.caskm.souvenirshopsystem.domain.dto.ChangePasswordDTO;
import cn.caskm.souvenirshopsystem.domain.dto.LoginFormDTO;
import cn.caskm.souvenirshopsystem.domain.dto.StudentUserUpdateDTO;
import cn.caskm.souvenirshopsystem.domain.vo.StudentUserVO;
import cn.caskm.souvenirshopsystem.service.StudentUserService;
import cn.caskm.souvenirshopsystem.utils.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/studentUser")
@RequiredArgsConstructor
public class StudentUserController {

    private final StudentUserService studentUserService;

    @PostMapping("/login")
    public Result<String> register(@RequestBody LoginFormDTO body) {
        return studentUserService.login(body);
    }

    @GetMapping("/profile")
    public Result<StudentUserVO> getProfile() {
        Long userId = UserContext.getUser();
        return studentUserService.getStudentUserDetail(userId.intValue());
    }

    @PutMapping("/update")
    public Result<Void> updateProfile(@RequestBody StudentUserUpdateDTO updateDTO) {
        Long userId = UserContext.getUser();
        updateDTO.setId(userId.intValue());
        return studentUserService.updateStudentUser(updateDTO);
    }

    @GetMapping("/detail/{id}")
    public Result<StudentUserVO> getDetail(@PathVariable("id") Integer id) {
        return studentUserService.getStudentUserDetail(id);
    }

    @GetMapping("/getByStudentNo/{sno}")
    public Result<StudentUserVO> getByStudentNo(@PathVariable("sno") String sno) {
        return studentUserService.getByStudentNo(sno);
    }

    @PutMapping("/changePassword")
    public Result<Void> changePassword(@RequestBody ChangePasswordDTO changePasswordDTO) {
        Long userId = UserContext.getUser();
        return studentUserService.changePassword(
                userId.intValue(),
                changePasswordDTO.getOldPassword(),
                changePasswordDTO.getNewPassword(),
                changePasswordDTO.getConfirmPassword()
        );
    }

}
